<?php
namespace Swiftx\Bundle\WeChat\Controller\Company;
use Swiftx\Bundle\WeChat\Interfaces\Common\AjaxController;
use Swiftx\Bundle\WeChat\Interfaces\Controller\Company\Store as StoreInterface;
use Swiftx\Bundle\WeChat\Factory\Company as CompanyFactory;
use Swiftx\Interfaces\Httpd\Request;
use Swiftx\Interfaces\Httpd\Response;

/**
 * 用户登录页面实现
 */
class Store extends AjaxController implements StoreInterface {

    /**
     * 企业工厂实例
     * @var CompanyFactory
     * @auto-injection
     */
    protected $companyFactory;

    /**
     * 更新店铺信息
     * @param Request $request
     * @param Response $response
     * @return bool
     */
    public function update(Request $request, Response $response):bool {
        // 获取当前用户
        $company = $this->companyFactory->currentLogin($request);
        $company->setTitle($request->getPost()->get('title'));
        $company->setIntroduce($request->getPost()->get('introduce'));
        $company->setDefaultAlbum($request->getPost()->get('defaultAlbum'));
        // 执行数据写入
        $this->entityManager->save($company);
        $this->entityManager->flush();
        // 返回执行结果
        return $this->successDisplay($response);
    }

    /**
     * 拉取店铺信息
     * @param Request $request
     * @param Response $response
     * @return bool
     */
    public function info(Request $request, Response $response):bool {
        // 获取当前用户
        $company = $this->companyFactory->currentLogin($request);
        // 返回执行结果
        return $this->successDisplay($response,[
            'title' => $company->getTitle(),
            'introduce' => $company->getIntroduce(),
            'defaultAlbum' => $company->getDefaultAlbumID()
        ]);
    }

}